Solution for using `this` keyword in ajax calls within methods?
Posted
by
dqhendricks
on Stack Overflow
See other posts from Stack Overflow
or by dqhendricks
Published on 2011-11-17T17:46:57Z
Indexed on
2011/11/17
17:51 UTC
Read the original article
Hit count: 176
JavaScript
|jQuery
I am creating a JavaScript class. Some of the methods contain AJAX calls using JQuery. The problem I am comming accross is that I cannot use the this
keyword within the AJAX callbacks due to a change in scope. I have come up with a hackey solution, but I am wondering what is the best practice way around this?
Here is an example:
var someClass = function() {
var someElement = $('form');
this.close = function() {
someElement.remove();
};
this.query = function() {
$.ajax({
url: someurl,
success: function() {
this.close(); // does not work because `this` is no longer the parent class
}
});
};
};
© Stack Overflow or respective owner